<?php

require FCPATH . '/application/controllers/backend/backend.php';

class Client extends Backend {

    public function __construct() {
	parent::__construct();
	$this->load->model('client_model', 'model');
    }

    public function index() {
	$this->load->model(array('city_model','service_model'));
	
	$markets_columns = array('id','menu_zh','menu_en','menu_fr');
	$markets_conditions = array(
	    array('where','category',2)
	);

	$data = array(
	    'cities' => $this->city_model->records(),
	    'markets' => $this->service_model->records($markets_columns,$markets_conditions)
	);
	
	$this->load_js(array('jquery-ui-1.8.20.custom.min', 'backend_client'));
	$this->load_css(array('custom-theme/jquery-ui-1.8.20.custom'));
	$this->rend('client',$data);
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$page_size = $page_size ? $page_size : 10;
	$offset = $page_size * ($page - 1);

	$conditions = array();
	$id = intval($this->input->get('id'));
	if (!empty($id)) {
	    $conditions[] = array('where', 'id', $id);
	}

	$market = intval($this->input->get('market'));
	if (!empty($market)) {
	    $conditions[] = array('where', 'market', $market);
	}
	
	$city = intval($this->input->get('city'));
	if (!empty($city)) {
	    $conditions[] = array('where', 'city', $city);
	}

	$name = trim($this->input->get('name'));
	if (!empty($name)) {
	    $conditions[] = array('or_like', 'name_zh', $name);
	    $conditions[] = array('or_like', 'name_fr', $name);
	    $conditions[] = array('or_like', 'name_en', $name);
	}

	$start = $this->input->get('start');
	if (!empty($start)) {
	    $conditions[] = array('where', 'created_at', $start);
	}

	$end = $this->input->get('end');
	if (!empty($end)) {
	    $conditions[] = array('where', 'created_at', $end);
	}

	$order_fields = array('id', 'city', 'name_zh', 'name_fr', 'name_en', 'created_at');
	$order_field = intval($this->input->get('order_field'));
	$order_type = intval($this->input->get('order_type'));
	$orders = array();
	if ($order_field >= 0 && $order_field < count($order_fields)) {
	    $orders = array(
		array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
	    );
	}
	$this->json_back('XT001', '', array(
	    'total' => $this->model->total($conditions),
	    'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
	));
    }

    public function add() {
	$this->load->model(array('city_model','service_model'));
	
	$markets_columns = array('id','menu_zh','menu_en','menu_fr');
	$markets_conditions = array(
	    array('where','category',2)
	);

	$data = array(
	    'cities' => $this->city_model->records(),
	    'markets' => $this->service_model->records($markets_columns,$markets_conditions)
	);

	$this->load_js('backend_client_form');
	$this->load_css('backend_client_form');
	$this->rend('client_add', $data);
    }

    public function insert() {
	$data = array(
	    'market' => intval($this->input->post('market')),
	    'city' => intval($this->input->post('city')),
	    'name_zh' => trim($this->input->post('name_zh')),
	    'name_fr' => trim($this->input->post('name_fr')),
	    'name_en' => trim($this->input->post('name_en')),
	    'content_zh' => $this->input->post('content_zh'),
	    'content_fr' => $this->input->post('content_fr'),
	    'content_en' => $this->input->post('content_en')
	);
	
	$this->validate($data);
	
	if (($id = $this->model->insert($data)) > 0) {
	    $this->json_back('XT001', '添加成功');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    
     public function edit() {
	 $id = intval($this->uri->segment(4));
	 
	$this->load->model(array('city_model','service_model'));
	
	$markets_columns = array('id','menu_zh','menu_en','menu_fr');
	$markets_conditions = array(
	    array('where','category',2)
	);

	$data = array(
	    'cities' => $this->city_model->records(),
	    'markets' => $this->service_model->records($markets_columns,$markets_conditions),
	    'client' => $this->model->find($id)
	);

	$this->load_js('backend_client_form');
	$this->load_css('backend_client_form');
	$this->rend('client_edit', $data);
    }
    
    public function update() {
	$id = intval($this->input->post('id'));
	
	$data = array(
	    'market' => intval($this->input->post('market')),
	    'city' => intval($this->input->post('city')),
	    'name_zh' => trim($this->input->post('name_zh')),
	    'name_fr' => trim($this->input->post('name_fr')),
	    'name_en' => trim($this->input->post('name_en')),
	    'content_zh' => $this->input->post('content_zh'),
	    'content_fr' => $this->input->post('content_fr'),
	    'content_en' => $this->input->post('content_en')
	);
	
	$this->validate($data);
	
	if ($this->model->update($id, $data)) {
	    $this->json_back('XT001', '更新成功');
	}
	
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }

    public function delete() {
	$id = intval($this->input->get('id'));
	if ($this->model->delete($id)) {
	    $this->json_back('XT001', '删除成功。');
	}
	$this->json_back('XT002', '数据库或SQL语句有误。');
    }
    
    private function validate($data){
	if(empty($data['city'])){
	    $this->json_back('XT002','城市选择有误');
	}
	
	if(empty($data['name_zh'])){
	    $this->json_back('XT002','请填写客户名称（中文）');
	}
	
	if(empty($data['name_fr'])){
	    $this->json_back('XT002','请填写客户名称（法文）');
	}
	
	if(empty($data['name_en'])){
	    $this->json_back('XT002','请填写客户名称（英文）');
	}
	
	if(empty($data['content_zh'])){
	    $this->json_back('XT002','请填写合作项目（中文）');
	}
	
	if(empty($data['content_fr'])){
	    $this->json_back('XT002','请填写合作项目（法文）');
	}
	
	if(empty($data['content_en'])){
	    $this->json_back('XT002','请填写合作项目（英文）');
	}
    }

}
